/*
 * Teknei 2014 - All rights reserved
 */
package com.teknei.acabus.core.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.collections.CollectionUtils;

import com.teknei.acabus.FacadeException;
import com.teknei.acabus.core.common.MessageConstants;
import com.teknei.acabus.core.entities.NavigationParent;
import com.teknei.acabus.core.facade.NavigationFacade;

/**
 * Controller for NavigationParent admin
 * @author Jorge Amaro
 * @version 1.0
 * @since 1.0
 *
 */
@ViewScoped
@ManagedBean(name="np_controller")
public class NavigationParentController extends GenericController implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private NavigationFacade facadeNavigation;
	private List<NavigationParent> itemsList;
	private NavigationParent newItem;
	private NavigationParent currentItem;
	private String input_new_text;
	private String input_new_outcome;
	private String input_new_url;
	private String input_edit_text;
	private String input_edit_outcome;
	private String input_edit_url;
	

	/**
	 * PostConstruct method. Initialize elements
	 */
	@PostConstruct
	private void postConstruct() {
		initItemList();
	}

	/**
	 * Finds all the elements
	 * @return
	 */
	public List<NavigationParent> findAll() {
		itemsList = null;
		initItemList();
		return itemsList;
	}

	/**
	 * Initialize items
	 * 
	 */
	private void initItemList() {
		if (CollectionUtils.isEmpty(itemsList)) {
			try {
				itemsList = facadeNavigation.findNavigationParents();
			} catch (FacadeException e) {
				super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
			}
		}
	}

	/**
	 * Refresh lists
	 */
	private void refreshItemsList() {
		itemsList = null;
		initItemList();
		newItem = null;
		currentItem = null;
	}

	/**
	 * Refresh inputs
	 */
	private void refreshInputs() {
		input_new_outcome= "";
		input_new_text = "";
		input_new_url = "";
		input_edit_outcome= "";
		input_edit_text = "";
		input_edit_url = "";
	}

	/**
	 * Saves the instance
	 */
	public void onSaveNavigationParent() {
		newItem = new NavigationParent();
		newItem.setOutcome(input_new_outcome);
		newItem.setText(input_new_text);
		newItem.setUrl(input_new_url);
		try {
			int pk = facadeNavigation.saveNavigationParent(newItem);
			newItem.setIdParent(pk);
			itemsList.add(newItem);
			newItem = null;
			refreshInputs();
			super.addInfoMessage(MessageConstants.ACTION_SAVE_ITEM_SUCCESS);
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_SAVE_ITEM_ERROR);
		}
	}

	/**
	 * Cancels action and refresh inputs
	 */
	public void onSaveCancelNavigationParent() {
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	/**
	 * Updates the instance
	 */
	public void onEditNavigationParent() {
		boolean changes = false;
		if(input_edit_outcome != null && input_edit_outcome.trim().length() > 1){
			changes = true;
			currentItem.setOutcome(input_edit_outcome);
		}
		if(input_edit_text != null && input_edit_text.trim().length() > 1){
			changes = true;
			currentItem.setText(input_edit_text);
		}
		if(input_edit_url != null && input_edit_url.trim().length() > 1){
			changes = true;
			currentItem.setUrl(input_edit_url);
		}
		if(!changes){
			super.addWarningMessage(MessageConstants.WARNING_EMPTY_UPDATE_FIELDS);
			return;
		}
		try {
			facadeNavigation.updateNavigationParent(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_UPDATE_SUCCESSFUL);
			currentItem = null;
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_UPDATE_FAILURE);
		}
	}

	/**
	 * Cancels the update and refresh inputs
	 */
	public void onEditCancelNavigationParent() {
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	/**
	 * Deletes the instance
	 */
	public void onDeleteNavigationParent() {
		try {
			facadeNavigation.deleteNavigationParent(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_DELETE_ITEM_SUCCESS,
					MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		} catch (FacadeException e) {
			refreshItemsList();
			super.addErrorMessage(MessageConstants.ACTION_DELETE_ITEM_ERROR,
					MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		}
	}

	/**
	 * Cancels the action
	 */
	public void onDeleteCancelNavigationParent() {
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}
	
	
	/**
	 * @return the facadeNavigation
	 */
	public NavigationFacade getFacadeNavigation() {
		return facadeNavigation;
	}
	/**
	 * @param facadeNavigation the facadeNavigation to set
	 */
	public void setFacadeNavigation(NavigationFacade facadeNavigation) {
		this.facadeNavigation = facadeNavigation;
	}
	/**
	 * @return the itemsList
	 */
	public List<NavigationParent> getItemsList() {
		initItemList();
		return itemsList;
	}
	/**
	 * @param itemsList the itemsList to set
	 */
	public void setItemsList(List<NavigationParent> itemsList) {
		this.itemsList = itemsList;
	}
	/**
	 * @return the newItem
	 */
	public NavigationParent getNewItem() {
		return newItem;
	}
	/**
	 * @param newItem the newItem to set
	 */
	public void setNewItem(NavigationParent newItem) {
		this.newItem = newItem;
	}
	/**
	 * @return the currentItem
	 */
	public NavigationParent getCurrentItem() {
		return currentItem;
	}
	/**
	 * @param currentItem the currentItem to set
	 */
	public void setCurrentItem(NavigationParent currentItem) {
		this.currentItem = currentItem;
	}
	/**
	 * @return the input_new_text
	 */
	public String getInput_new_text() {
		return input_new_text;
	}
	/**
	 * @param input_new_text the input_new_text to set
	 */
	public void setInput_new_text(String input_new_text) {
		this.input_new_text = input_new_text;
	}
	/**
	 * @return the input_new_outcome
	 */
	public String getInput_new_outcome() {
		return input_new_outcome;
	}
	/**
	 * @param input_new_outcome the input_new_outcome to set
	 */
	public void setInput_new_outcome(String input_new_outcome) {
		this.input_new_outcome = input_new_outcome;
	}
	/**
	 * @return the input_new_url
	 */
	public String getInput_new_url() {
		return input_new_url;
	}
	/**
	 * @param input_new_url the input_new_url to set
	 */
	public void setInput_new_url(String input_new_url) {
		this.input_new_url = input_new_url;
	}
	/**
	 * @return the input_edit_text
	 */
	public String getInput_edit_text() {
		return input_edit_text;
	}
	/**
	 * @param input_edit_text the input_edit_text to set
	 */
	public void setInput_edit_text(String input_edit_text) {
		this.input_edit_text = input_edit_text;
	}
	/**
	 * @return the input_edit_outcome
	 */
	public String getInput_edit_outcome() {
		return input_edit_outcome;
	}
	/**
	 * @param input_edit_outcome the input_edit_outcome to set
	 */
	public void setInput_edit_outcome(String input_edit_outcome) {
		this.input_edit_outcome = input_edit_outcome;
	}
	/**
	 * @return the input_edit_url
	 */
	public String getInput_edit_url() {
		return input_edit_url;
	}
	/**
	 * @param input_edit_url the input_edit_url to set
	 */
	public void setInput_edit_url(String input_edit_url) {
		this.input_edit_url = input_edit_url;
	}
	
	
	
}
